import { CHECC_API_BASE_DATA, CHECC_API_CMS, CHECC_API_FAST_DFS } from '@/constants/proxy'
import request from '@/utils/request'
import { isEmpty } from 'lodash'
import { ObjectType, Pagination } from 'typings/interfaces'

export function getRoutes({ method, params, data }: any): Promise<any> {
  return request({
    url: CHECC_API_CMS + '/menu/getRouters',
    method,
    params,
    data,
  })
}

export function requestOrgTree(params: any): Promise<any> {
  return request({
    url: CHECC_API_CMS + `/org/tree/by-unit-type`,
    method: 'get',
    params,
  })
}

export function requestOrgTree2(): Promise<any> {
  return request({
    url: CHECC_API_CMS + `/org/getTreeForDataBase`,
    method: 'get',
  })
}

export function requestRegionTree(data: any): Promise<any> {
  return request({
    url: CHECC_API_CMS + `/region/regionTree`,
    method: 'post',
    data,
  })
}

export function getTreeByLoginOrg(): Promise<any> {
  return request({
    url: CHECC_API_CMS + `/org/getTreeByLoginOrg`,
    method: 'get',
  })
}

// 上传文件
export function uploadFile(data): Promise<any> {
  return request({
    url: CHECC_API_FAST_DFS + '/file/upload',
    method: 'post',
    data,
  })
}

// 获取上传文件信息
export function requestFileInfo(data): Promise<any> {
  return request({
    url: CHECC_API_FAST_DFS + '/file_info/' + data.id,
    method: 'get',
  })
}

// 获取用户数据
export const getUser = () =>
  request({
    url: CHECC_API_CMS + '/user/info',
    method: 'get',
  })

/**
 * 路段分页查询
 * @param pagination 分页
 * @param data 参数
 * @returns
 */
export const getBdSection = (pagination: Omit<Pagination, 'total'>, data: ObjectType) =>
  request({
    url: CHECC_API_BASE_DATA + '/bd-fixed-section/page',
    method: 'post',
    params: pagination,
    data,
  })

/**
 * 路线分页查询
 * @param pagination 分页
 * @param data 参数
 * @returns
 */
export const getRoadRequest = (pagination: Omit<Pagination, 'total'>, data: ObjectType) =>
  request({
    url: CHECC_API_BASE_DATA + '/bdRoute2/page',
    method: 'post',
    params: pagination,
    data,
  })

// 所有路段信息
export function requestBdSectionAll(data: any) {
  return request({
    url: CHECC_API_BASE_DATA + '/bd-fixed-section/list',
    method: 'post',
    data: data,
  })
}

/** 
 * 根据所属运营公司id获路段下拉数据
 */
export const requestSectionOptions = async (operationCompanyId: string) => {
  const { data: { data }, } = await requestBdSectionAll({ operationCompanyId })
  if (isEmpty(data)) {
    return []
  }
  return data.map((item: ObjectType) => ({
    ...item,
    label: item.sectionAbbr,
    value: item.sectionAbbr,
  }))
}

// 获取收费里程
export function requestBdTollMile(data: ObjectType): Promise<any> {
  return request({
    url: CHECC_API_BASE_DATA + '/bd-fixed-section/gettollMile',
    method: 'post',
    data,
  })
}
